package com.nulldev.util.security.hashAPI.impls.murmur;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import com.nulldev.util.security.hashAPI.Hashable;

public class Murmur1 implements Hashable<Long> {
	private static final long MURMUR_SEED = 0x7f3a21eal;

	protected static byte[] longToBytes(long x) {
		ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
		// The ByteOrder.LITTLE_ENDIAN format matches the Google Guava toString() format
		buffer.order(ByteOrder.LITTLE_ENDIAN);
		buffer.putLong(x);
		return buffer.array();
	}

	@Override
	public Long hash(final byte[] data) {
		return com.nulldev.util.security.hashAPI.impls.murmur.base.Murmur1.hashAligned(data, data.length, MURMUR_SEED);
	}

	@Override
	public byte[] raw_hash(byte[] data) {
		return longToBytes(com.nulldev.util.security.hashAPI.impls.murmur.base.Murmur1.hashAligned(data, data.length, MURMUR_SEED));
	}

	@Override
	public String id() {
		return "native.murmur1";
	}

	@Override
	public String[] hashes() {
		return new String[]
			{ "murmur", "Murmur", "Murmur1", "murmur1" };
	}

	@Override
	public Hashable<?> clone() {
		return this;
	}
}
